import urllib
import cStringIO 
import Image
import math

def get_static_google_map(filename_wo_extension, center=None, zoom=None, imgsize="500x500", imgformat="jpeg",
                          maptype="roadmap", markers=None ):  
    """retrieve a map (image) from the static google maps server 
    
     See: http://code.google.com/apis/maps/documentation/staticmaps/
        
        Creates a request string with a URL like this:
        http://maps.google.com/maps/api/staticmap?center=Brooklyn+Bridge,New+York,NY&zoom=14&size=512x512&maptype=roadmap
&markers=color:blue|label:S|40.702147,-74.015794&sensor=false"""
   
    
    # assemble the URL
    request =  "http://maps.google.com/maps/api/staticmap?" # base URL, append query params, separated by &
   
    # if center and zoom  are not given, the map will show all marker locations
    if center != None:
        request += "center=%s&" % center
        #request += "center=%s&" % "40.714728, -73.998672"   # latitude and longitude (up to 6-digits)
        #request += "center=%s&" % "50011" # could also be a zipcode,
        #request += "center=%s&" % "Brooklyn+Bridge,New+York,NY"  # or a search term 
    if center != None:
        request += "zoom=%i&" % zoom  # zoom 0 (all of the world scale ) to 22 (single buildings scale)


    request += "size=%ix%i&" % (imgsize)  # tuple of ints, up to 640 by 640
    request += "format=%s&" % imgformat
    request += "maptype=%s&" % maptype  # roadmap, satellite, hybrid, terrain


    # add markers (location and style)
    if markers != None:
        for marker in markers:
                request += "%s&" % marker


    #request += "mobile=false&"  # optional: mobile=true will assume the image is shown on a small screen (mobile device)
    request += "sensor=false&"   # must be given, deals with getting loction from mobile device 
    print request
    
    urllib.urlretrieve(request, filename_wo_extension+"."+imgformat) # Option 1: save image directly to disk
    
    # Option 2: read into PIL 
    web_sock = urllib.urlopen(request)
    imgdata = cStringIO.StringIO(web_sock.read()) # constructs a StringIO holding the image
    try:
        PIL_img = Image.open(imgdata)
    
    # if this cannot be read as image that, it's probably an error from the server,
    except IOError:
        print "IOError:", imgdata.read() # print error (or it may return a image showing the error"
     
    # show image 
    #else:
        #PIL_img.show()
        #PIL_img.save(filename_wo_extension+".png", "PNG") # save as jpeg


offset=268435456
offset=float(offset)
radius=offset/math.pi 

def LToX(x):
    return round(offset+radius*x*math.pi/180)

def LToY(y):
    return round(offset-radius*math.log((1+math.sin(y*math.pi/180))/(1-math.sin(y*math.pi/180)))/2)

def XToL(x):
    return ((round(x)-offset)/radius)*180/math.pi

def YToL(y):
    return (math.pi/2-2*math.atan(math.exp((round(y)-offset)/radius)))*180/math.pi


#  X = X pixel offset of new map center from old map center
#  Y = Y pixel offset of new map center from old map center
#  x = Longitude of map center
#  y = Latitude  of map center
#  z = Zoom level

#  result.x = Longitude of adjusted map center
#  result.y = Latitude  of adjusted map center
def adjust(X=0,Y=0,x=52.38,y=4,z=12):
    newX = XToL(LToX(x) + (X<<(20-z)))
    newY = YToL(LToY(y) + (Y<<(21-z)))
    return (newX,newY)

if __name__ == '__main__':


    # make a map around a center
    get_static_google_map("amsterdam", center="52.38,4.90", zoom=12, imgsize=(800,800),
                          imgformat="png", maptype="satellite" )


